require 'rexml/document'
module FamilyTreeApi
  module Person
    module Assertion
      
      class Person
        attr_accessor :ref
        
        def initialize(options = {})
          @notes = []
          @ref = options[:ref]
        end
        def parse_attributes(element)
          @ref = element.attributes["ref"]
        end
        
        def to_xml
          e = REXML::Element.new(self.person_type)
          e.add_attribute('ref', @ref) if @ref
          e
        end
        
        def ==(other)
          self.ref == other.ref
        end
        
        def person_type
          self.class.to_s.split('::').last.downcase
        end

        def self.parse(person_element)
          unless person_element.nil?
            person = self.new
            person.parse_attributes(person_element)
            person
          end
        end
      end

      class Parent < Assertion::Person
      end

      class Child < Assertion::Person
      end

      class Spouse < Assertion::Person
        attr_accessor :role
        
        def initialize(options = {})
          super(options)
          @role = options[:role]
        end
        
        def self.parse(spouse_element)
          unless spouse_element.nil?
            spouse = self.new
            spouse.role = spouse_element.attributes["role"]
            spouse.parse_attributes(spouse_element)
            spouse
          end
        end
        
        def to_xml
          e = super
          e.add_attribute('role', @role)
          e
        end
        
        def ==(other)
          super(other) &&
            self.role == other.role
        end
      end

    end
  end
end